package service

import (
	"encoding/json"
	"errors"
	"fmt"
	ClickHouse "gin/database"
	"gin/types"
	"github.com/gin-gonic/gin"
	teamones_helper "github.com/teamones-open/go-helper"
	"strconv"
	"time"
)

type Option struct {
}

var tr TeamonesRequest

// GetOptionConfigItem 读取配置
func (t *Option) GetOptionConfigItem(ctx *gin.Context, optionName string, value interface{}) error {
	var req = types.GetOptionConfigItemReq{
		OptionsName: optionName,
	}

	var headers = map[string]string{"x-userinfo": GetStringFromContext(ctx, "x-userinfo")}

	err := tr.Do("teamones-saas", "options/get_options", req, headers, &value)
	if err != nil {
		return err
	}

	return nil
}

// GetProjectBiConfig 读取配置
func (t *Option) GetProjectBiConfig(ctx *gin.Context, projectId uint64) (res types.BiProjectConfig, err error) {
	var req = types.GetProjectBiConfigReq{
		Param: types.GetProjectBiConfigReqParam{
			Filter: types.GetProjectBiConfigReqFilter{
				Id: projectId,
			},
		},
	}
	var response types.BiProjectConfigRes
	var headers = map[string]string{"x-userinfo": GetStringFromContext(ctx, "x-userinfo")}
	err = tr.Do("teamones-saas", "bi/find_config", req, headers, &response)
	if err != nil {
		return
	}
	res = response.Data
	return
}

// GetBiConfigShowProjectIdList 读取配置显示的项目Id
func (t *Option) GetBiConfigShowProjectIdList(ctx *gin.Context) (res []int64, err error) {
	var response types.GetBiConfigShowProjectIdListRes
	var headers = map[string]string{"x-userinfo": GetStringFromContext(ctx, "x-userinfo")}
	err = tr.Do("teamones-saas", "bi/project_show_id_list", nil, headers, &response)
	if err != nil {
		return
	}
	return response.Data, err
}

// SelectBiProjectConfig 读取项目列表配置
func (t *Option) SelectBiProjectConfig(ctx *gin.Context, projectIdList []uint64) (res map[int64]types.BiProjectConfig, err error) {

	var projectIds string
	for _, projectId := range projectIdList {
		projectIds += strconv.FormatUint(projectId, 10) + ","
	}
	var req = types.SelectBiProjectConfigReq{
		Param: types.SelectBiProjectConfigReqParam{
			Filter: types.SelectBiProjectConfigReqParamFilter{Id: []string{"-in", projectIds}},
			Page:   []int{1, len(projectIdList)},
		},
	}
	var response types.SelectBiProjectConfigRes
	var headers = map[string]string{"x-userinfo": GetStringFromContext(ctx, "x-userinfo")}
	var serviceName = "teamones-saas"
	var endpoint = "bi/select_config"
	err = tr.Do(serviceName, endpoint, req, headers, &response)
	if err != nil {
		return
	}
	if response.Code < 0 {
		err = errors.New(fmt.Sprintf("[%s:%s] %s", serviceName, endpoint, response.Msg))
		return
	}
	res = make(map[int64]types.BiProjectConfig, 0)

	for i, datum := range response.Data {
		res[datum.ID] = response.Data[i]
	}

	return
}

// GetProjectShowStepCategoryListConfig 读取配置
func (t *Option) GetProjectShowStepCategoryListConfig(ctx *gin.Context) (res types.GetProjectShowStepCategoryListConfigRes, err error) {
	var req = types.GetProjectShowStepCategoryListConfigReq{}
	req.Param.ProjectId = 0

	reqStr, _ := json.Marshal(req)
	rk := fmt.Sprintf("teamones-saas:get_show_step_category_list:%s", teamones_helper.GenerateMd5(string(reqStr)))
	val, _ := ClickHouse.Rdb.Get(rk).Result()
	if val != "" {
		err = json.Unmarshal([]byte(val), &res)
		if err == nil {
			return
		}
	}
	var headers = map[string]string{"x-userinfo": GetStringFromContext(ctx, "x-userinfo")}
	err = tr.Do("teamones-saas", "project/get_show_step_category_list", req, headers, &res)
	if err != nil {
		return
	}

	rs, err := json.Marshal(res)
	if err != nil {
		return
	}
	_, err = ClickHouse.Rdb.Set(rk, rs, time.Second*60).Result()
	if err != nil {
		return
	}

	return
}

// GetTaskReviewTagConfig 读取配置
func (t *Option) GetTaskReviewTagConfig(ctx *gin.Context) (res types.GetTaskReviewTagConfigRes, err error) {
	var req = types.GetTaskReviewTagConfigReq{}
	req.OptionsName = "plan_todo_review_tag"
	req.OptionsItemName = "default"
	reqStr, _ := json.Marshal(req)
	rk := fmt.Sprintf("teamones-saas:GetTaskReviewTagConfig:%s", teamones_helper.GenerateMd5(string(reqStr)))
	err = GetCachedJson(rk, &res)
	if err == nil {
		return
	}
	err = nil
	defer func() {
		_ = SetCacheJsonExpire(rk, &res, time.Minute*3)
	}()
	var headers = map[string]string{"x-userinfo": GetStringFromContext(ctx, "x-userinfo")}
	err = tr.Do("teamones-saas", "options/get_options_item", req, headers, &res)
	return
}

// GetTaskReviewTagConfig 读取配置
func (t *Option) GetProjectReviewOptions(ctx *gin.Context, projectId int64) (res []types.GetProjectReviewOptionsResData, err error) {
	var req = types.GetProjectReviewOptionsReq{}
	req.Param.Filter.ProjectId = projectId
	//reqStr, _ := json.Marshal(req)
	//rk := fmt.Sprintf("teamones-saas:GetTaskReviewTagConfig:%s", teamones_helper.GenerateMd5(string(reqStr)))
	//err = GetCachedJson(rk, &res)
	//if err == nil {
	//	return
	//}
	//err = nil
	//defer func() {
	//	_ = SetCacheJsonExpire(rk, &res, time.Minute*3)
	//}()
	var headers = map[string]string{"x-userinfo": GetStringFromContext(ctx, "x-userinfo")}
	result := types.GetProjectReviewOptionsRes{}

	err = tr.Do("teamones-saas", "project_review_options/select_project_review_options", req, headers, &result)
	if err != nil {
		return
	}
	fmt.Println(result)
	res = result.Data
	return
}
